package com.xiaoshuidi.cloud.module.rooms.controller.admin.doorlockcdkey;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.doorlockcdkey.vo.*;
import com.xiaoshuidi.cloud.module.rooms.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.rooms.service.RoomDoorlockCdkeyService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 房间门锁激活码配置
 *
 * @Author Feng
 * @date 2023/9/27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping(ApiConstants.NAME + "/doorlock/cdkey")
public class RoomDoorlockCdkeyController {

    private final RoomDoorlockCdkeyService roomDoorlockCdkeyService;

    /**
     * 门锁激活码配置 - 新增
     *
     * @param req
     * @return
     */
    @PostMapping("/create")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:doorlock:cdkey:create')")
    public CommonResult<Boolean> create(@RequestBody @Valid RoomDoorlockCdkeyReq req) {
        return CommonResult.success(this.roomDoorlockCdkeyService.create(req));
    }

    /**
     * 门锁激活码配置 - 编辑
     *
     * @param req
     * @return
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:doorlock:cdkey:update')")
    public CommonResult<Boolean> update(@RequestBody @Valid RoomDoorlockCdkeyReq req) {
        return CommonResult.success(this.roomDoorlockCdkeyService.update(req));
    }

    /**
     * 门锁激活码配置 - 分页列表
     *
     * @param req
     * @return
     */
    @PostMapping("/page")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:doorlock:cdkey:query')")
    public CommonResult<PageResult<RoomDoorlockCdkeyPageResp>> page(@RequestBody @Valid RoomDoorlockCdkeyPageReq req) {
        return CommonResult.success(this.roomDoorlockCdkeyService.page(req));
    }

    /**
     * 门锁激活码配置 - 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/remove")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:doorlock:cdkey:remove')")
    public CommonResult<Boolean> remove(@RequestParam("id") @Valid @NotNull(message = "id不能为空") Long id) {
        return CommonResult.success(this.roomDoorlockCdkeyService.remove(id));
    }

    /**
     * 门锁激活码配置 - 关联房源
     *
     * @param req
     * @return
     */
    @PostMapping("/edit/mapping")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:doorlock:cdkey:edit:mapping')")
    public CommonResult<Boolean> editMapping(@RequestBody @Valid RoomDoorlockCdkeyMappingReq req) {
        return CommonResult.success(this.roomDoorlockCdkeyService.editMapping(req));
    }

    /**
     * 门锁激活码配置 - 查询关联房源
     *
     * @param id
     * @return
     */
    @GetMapping("/query/mapping")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:doorlock:cdkey:query')")
    public CommonResult<RoomDoorlockCdkeyMappingResp> queryMapping(@RequestParam("id") @Valid @NotNull(message = "id不能为空") Long id) {
        return CommonResult.success(this.roomDoorlockCdkeyService.queryMapping(id));
    }

}
